#!/usr/bin/python
# -*- coding: UTF-8 -*-
from django.utils.deprecation import MiddlewareMixin
from django.http import JsonResponse
from utils.errors import genErrors
import json
import ast
from branch.urls import *
from user.urls import *
from report.urls import *

formDct = {
    testAPI.urlPrefix + testAPI.moduleUrl: testAPI.form,
    test1API.urlPrefix + test1API.moduleUrl: test1API.form,
    signupAPI.urlPrefix + signupAPI.moduleUrl: signupAPI.form,
    generateInviteCodeAPI.urlPrefix + generateInviteCodeAPI.moduleUrl: generateInviteCodeAPI.form,
    loginnowxAPI.urlPrefix + loginnowxAPI.moduleUrl: loginnowxAPI.form,
    changePasswordAPI.urlPrefix + changePasswordAPI.moduleUrl: changePasswordAPI.form,
    updatezsdyAPI.urlPrefix + updatezsdyAPI.moduleUrl: updatezsdyAPI.form,
    loginAPI.urlPrefix + loginAPI.moduleUrl: loginAPI.form,
    logoutAPI.urlPrefix + logoutAPI.moduleUrl: logoutAPI.form,
    getDYAPI.urlPrefix + getDYAPI.moduleUrl: getDYAPI.form,
    updateOtherAPI.urlPrefix + updateOtherAPI.moduleUrl: updateOtherAPI.form,
    submitReportAPI.urlPrefix + submitReportAPI.moduleUrl: submitReportAPI.form,
    submitCheckAPI.urlPrefix + submitCheckAPI.moduleUrl: submitCheckAPI.form,
    getReportListAPI.urlPrefix + getReportListAPI.moduleUrl: getReportListAPI.form,
    deleteRpAPI.urlPrefix + deleteRpAPI.moduleUrl: deleteRpAPI.form,
    getReportContentAPI.urlPrefix + getReportContentAPI.moduleUrl: getReportContentAPI.form,
    getCheckAPI.urlPrefix + getCheckAPI.moduleUrl: getCheckAPI.form,
    newDailyAPI.urlPrefix + newDailyAPI.moduleUrl: newDailyAPI.form,
    getDailyListAPI.urlPrefix + getDailyListAPI.moduleUrl: getDailyListAPI.form,
    deleteDailyAPI.urlPrefix + deleteDailyAPI.moduleUrl: deleteDailyAPI.form,
    getContectAPI.urlPrefix + getContectAPI.moduleUrl: getContectAPI.form,
    saveArticleAPI.urlPrefix + saveArticleAPI.moduleUrl: saveArticleAPI.form,
    getArticleAPI.urlPrefix + getArticleAPI.moduleUrl: getArticleAPI.form,
    submitThinkAPI.urlPrefix + submitThinkAPI.moduleUrl: submitThinkAPI.form,
    getSingleThinkAPI.urlPrefix + getSingleThinkAPI.moduleUrl: getSingleThinkAPI.form,
    checkNotSubmitAPI.urlPrefix + checkNotSubmitAPI.moduleUrl: checkNotSubmitAPI.form,
    getThinksAPI.urlPrefix + getThinksAPI.moduleUrl: getThinksAPI.form,
    saveTestAPI.urlPrefix + saveTestAPI.moduleUrl: saveTestAPI.form,
    getTestAPI.urlPrefix + getTestAPI.moduleUrl: getTestAPI.form,





    getAliTokenAPI.urlPrefix + getAliTokenAPI.moduleUrl: getAliTokenAPI.form,
    
    }

methodDct = {
    testAPI.urlPrefix + testAPI.moduleUrl: testAPI.method,
    test1API.urlPrefix + test1API.moduleUrl: test1API.method,
    getDYAPI.urlPrefix + getDYAPI.moduleUrl: getDYAPI.method,
    signupAPI.urlPrefix + signupAPI.moduleUrl: signupAPI.method,
    generateInviteCodeAPI.urlPrefix + generateInviteCodeAPI.moduleUrl: generateInviteCodeAPI.method,
    loginnowxAPI.urlPrefix + loginnowxAPI.moduleUrl: loginnowxAPI.method,
    changePasswordAPI.urlPrefix + changePasswordAPI.moduleUrl: changePasswordAPI.method,
    updatezsdyAPI.urlPrefix + updatezsdyAPI.moduleUrl: updatezsdyAPI.method,
    updateOtherAPI.urlPrefix + updateOtherAPI.moduleUrl: updateOtherAPI.method,
    loginAPI.urlPrefix + loginAPI.moduleUrl: loginAPI.method,
    logoutAPI.urlPrefix + logoutAPI.moduleUrl: logoutAPI.method,
    submitReportAPI.urlPrefix + submitReportAPI.moduleUrl: submitReportAPI.method,
    submitCheckAPI.urlPrefix + submitCheckAPI.moduleUrl: submitCheckAPI.method,
    getReportListAPI.urlPrefix + getReportListAPI.moduleUrl: getReportListAPI.method,
    deleteRpAPI.urlPrefix + deleteRpAPI.moduleUrl: deleteRpAPI.method,
    getReportContentAPI.urlPrefix + getReportContentAPI.moduleUrl: getReportContentAPI.method,
    getCheckAPI.urlPrefix + getCheckAPI.moduleUrl: getCheckAPI.method,
    newDailyAPI.urlPrefix + newDailyAPI.moduleUrl: newDailyAPI.method,
    getDailyListAPI.urlPrefix + getDailyListAPI.moduleUrl: getDailyListAPI.method,
    deleteDailyAPI.urlPrefix + deleteDailyAPI.moduleUrl: deleteDailyAPI.method,
    getContectAPI.urlPrefix + getContectAPI.moduleUrl: getContectAPI.method,
    saveArticleAPI.urlPrefix + saveArticleAPI.moduleUrl: saveArticleAPI.method,
    getArticleAPI.urlPrefix + getArticleAPI.moduleUrl: getArticleAPI.method,
    submitThinkAPI.urlPrefix + submitThinkAPI.moduleUrl: submitThinkAPI.method,
    getSingleThinkAPI.urlPrefix + getSingleThinkAPI.moduleUrl: getSingleThinkAPI.method,
    checkNotSubmitAPI.urlPrefix + checkNotSubmitAPI.moduleUrl: checkNotSubmitAPI.method,
    getThinksAPI.urlPrefix + getThinksAPI.moduleUrl: getThinksAPI.method,
    saveTestAPI.urlPrefix + saveTestAPI.moduleUrl: saveTestAPI.method,
    getTestAPI.urlPrefix + getTestAPI.moduleUrl: getTestAPI.method,



    getAliTokenAPI.urlPrefix + getAliTokenAPI.moduleUrl: getAliTokenAPI.method,

}


class FormCheckMiddleware(MiddlewareMixin):
    def process_request(self, request):
        # 路径是否正确
        # pdb.set_trace()
        if (request.path in methodDct):
            # 检测方法
            if request.method != methodDct[request.path]:
                data = genErrors("WrongMethod")
                # logger.info(json.dumps(data))
                return JsonResponse(data)
            try:
                formInst = formDct[request.path](json.loads(request.body.decode('utf-8')))
            except:
                try:
                    if (request.method == "POST"):
                        formInst = formDct[request.path](request.POST)
                    else:
                        formInst = formDct[request.path](request.GET)
                except:
                    data = genErrors("InvalidForm")
                    return JsonResponse(data)
            if not formInst.is_valid():
                data1 = {
                    'code': 1,
                    # 'requestParam': ,
                }
                all_error = formInst.errors
                # all = formInst.errors.get('__all__')
                # print(all)
                if all_error:
                    for a in all_error:
                        # print(all_error[a][0])
                        # if all_error[a][0] in genErrors:
                        #     data1.append(genErrors(all_error[a][0]))
                        # else:
                        data1[a] = (all_error[a][0])
                return JsonResponse(data1)
            else:
                request.checkForm = formInst

